package com.example.wist.mediaplayer;

import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//        其实媒体扫描这个工作，Android设置已经替我们干了，Android系统会在SD卡有更新的时候
//        自动将SD卡文件分类（视频/音频/图片...）,并存入SQLite数据库，就保存在媒体存储器里面
//        （com.android.providers.media）
public class SearchMusic extends ActionBarActivity {

    TextView textView;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_music);

        List<HashMap<String, Object>> musicList = scanAllAudioFiles();

        //传结果回MainActivity
        //数据是使用Intent返回
        //将ArrayList<Map<String,Object>>
        // 封装成ArrayList，装包返回
        Intent intent = new Intent();
        Bundle b = new Bundle();
        ArrayList bundleList = new ArrayList();
        bundleList.add(musicList);
        b.putParcelableArrayList("result",bundleList);
        //把返回数据存入Intent
        intent.putExtras(b);
        //设置返回数据,1表示resultCode(相当于一个编号)
        SearchMusic.this.setResult(1, intent);
        //关闭Activity
        SearchMusic.this.finish();
        //播放
//        mediaPlayer = MediaPlayer.create(this, R.raw.valder);
//        mediaPlayer.start();
    }

    public ArrayList<HashMap<String, Object>> scanAllAudioFiles(){

        //生成动态数组，并且转载数据
        ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();

        //查询媒体数据库
        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        //遍历媒体数据库
        if(cursor.moveToFirst()){

            while (!cursor.isAfterLast()) {

                //歌曲编号
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));

                //歌曲标题
                String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));

                //歌曲的专辑名：MediaStore.Audio.Media.ALBUM
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));

                //歌曲的歌手名： MediaStore.Audio.Media.ARTIST
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));

                //歌曲文件的路径 ：MediaStore.Audio.Media.DATA
                String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));

                //歌曲的总播放时长 ：MediaStore.Audio.Media.DURATION
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));

                //歌曲文件的大小 ：MediaStore.Audio.Media.SIZE
                Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

                //歌曲的专辑图 ：MediaStore.Audio.Media.ALBUM_ID
                String album_id=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));



                if(size>1024*800){//大于800K
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("musicId", id);
                    map.put("artist",artist);
                    map.put("musicTitle", tilte);
                    map.put("musicFileUrl", url);
                    map.put("music_file_name", tilte);
                    map.put("album_id", album_id);
                    mylist.add(map);
                }
                cursor.moveToNext();
            }
        }

        return mylist;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_search_music, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
